#include <QToolBar>
#include <QAction>
#include <QUrl>
#include <QProcess>
#include <QVBoxLayout>

#include "imageitemproxy.h"
#include "qrwindow.h"
#include "imageitem.h"

class ImageItemProxyPrivate
{
public:
    ImageItemProxyPrivate(ImageItemProxy* q);

    ImageItemProxy* q_ptr;

    ImageItem* imageItem;
};

ImageItemProxyPrivate::ImageItemProxyPrivate(ImageItemProxy *q) : q_ptr(q)
{
    imageItem = new ImageItem(q_ptr);

    QVBoxLayout* layout = new QVBoxLayout;
    QToolBar* toolbar = new QToolBar(QObject::tr("Tools"), q_ptr);
    imageItem = new ImageItem(q_ptr);

    QAction* qrcodeAct = new QAction(q_ptr);
    qrcodeAct->setText(QObject::tr("Show QrCode"));
    toolbar->addAction(qrcodeAct);
    QObject::connect(qrcodeAct, SIGNAL(triggered()), q_ptr, SLOT(showQrCode()));

    QAction* locateAct = new QAction(q_ptr);
    locateAct->setText(QObject::tr("Locate"));
    toolbar->addAction(locateAct);
    QObject::connect(locateAct, SIGNAL(triggered()), q_ptr, SLOT(locate()));

    layout->addWidget(toolbar);
    layout->addWidget(imageItem);
    q_ptr->setLayout(layout);
}

ImageItemProxy::ImageItemProxy(QWidget *parent) : QWidget(parent)
{
    d_ptr = new ImageItemProxyPrivate(this);
}

ImageItemProxy::~ImageItemProxy()
{
    delete d_ptr;
}

void ImageItemProxy::setUrl(const QUrl& url)
{
    d_ptr->imageItem->setUrl(url);
}

QUrl ImageItemProxy::url() const
{
    return d_ptr->imageItem->url();
}

void ImageItemProxy::setDescription(const QString& desc)
{
    d_ptr->imageItem->setDescription(desc);
}

QString ImageItemProxy::description() const
{
    return d_ptr->imageItem->description();
}

void ImageItemProxy::showQrCode()
{
    Haha::QRWindow* qrw = Haha::QRWindow::instance();
    qrw->setUrl(d_ptr->imageItem->url());
    qrw->show();
}

void ImageItemProxy::locate()
{
#ifdef Q_OS_WIN
    QStringList args;
    args << QString("/select,%1").arg(d_ptr->imageItem->localPath());
    QProcess::startDetached("explorer.exe", args);
#endif
}

